<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 9, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

class AbstractFieldsControl extends Object {
	
	var $_arrayFields;
	var $_inputName;
	var $_dto;
	
	/**
	 * Constructor
	 *
	 * @param AbstractRubikDTO $dto
	 * @param array $arrayListField the list that will be generated. ex ( array('clipart_id' => '', 'clipart_keywords' = new BasicHtmlFieldInfo('clipart_keywords'))
	 * @param array $ignoreList
	 * @param string the globalName of all control
	 */
	function __construct($dto, $arrayListField = null, $ignoreList = null, $name = 'admin') {
		$this->_inputName = $name;
		$this->_dto = &$dto;
		$this->_arrayFields = &$this->_prepareArrayFields($arrayListField, $ignoreList);
	}
	
	/**
	 * Preapare array field data for creating
	 *
	 * @param array $arrayViewField
	 * @return array
	 */
	function &_prepareArrayFields($arrayViewField, $ignoreList) {
		$dto = $this->_dto;
		$dbInfo = RubikDBFactory::getDBInfo ( $dto );
		if (!$arrayViewField) {
			$arrayViewField = RubikHelper::getProperties($dto);
		}
		
		$ctrlName = $this->_inputName;
		
		$returnArr = array();
		
		foreach ($arrayViewField as $fieldName => $fieldRule) {
			$fieldInfo = $dbInfo->getDbField ( $fieldName );
			
			if ($ignoreList && in_array($fieldName, $ignoreList)) {
				continue;
			}
			if (!($fieldRule->isHtmlFieldInfo)) {
				$name = $this->_getControlName($fieldName);
				if ($fieldInfo && $fieldInfo->isBoolean()) {
					RCoreImporter::cls('html.fieldInfo.BooleanHtmlFieldInfo');
					$fieldRule = new BooleanHtmlFieldInfo($name, $fieldRule);
				}
				else if ($fieldInfo && $fieldInfo->dataType == SYM_TYPE_TEXT ) {
					RCoreImporter::cls('html.fieldInfo.TextBoxHtmlFieldInfo');
					$fieldRule = new TextBoxHtmlFieldInfo($name, $fieldRule);
				}
				else if ($fieldInfo && $fieldInfo->dataType == SYM_TYPE_MD5 ) {
					RCoreImporter::cls('html.fieldInfo.PasswordHtmlFieldInfo');
					$fieldRule = new PasswordHtmlFieldInfo($name, $fieldRule);
				}
				else {
					if ($fieldInfo && !$fieldInfo->isKey) {
						RCoreImporter::cls('html.fieldInfo.BasicHtmlFieldInfo');
						$fieldRule = new BasicHtmlFieldInfo($name, $fieldRule);
					}
				}
				if ($fieldInfo && !$fieldInfo->isKey) {
					$fieldRule->setMinMaxLength($fieldInfo->maxLength);
				}
				
				$returnArr[$fieldName] = $fieldRule;
			}
			$returnArr[$fieldName] = $fieldRule;
		}
		return $returnArr;
	}
	
	/**
	 * Remove the field name out of the list
	 *
	 * @param string $fieldName
	 */
	function removeField($fieldName) {
		unset($this->_arrayFields[$fieldName]);
	}
	
	/**
	 * Set the field information for a special field
	 *
	 * @param string $fieldName
	 * @param AbstractHtmlFieldInfo $fieldInfo
	 */
	function setFieldHtmlInfo($fieldName, $fieldInfo) {
		$this->_arrayFields[$fieldName] = $fieldInfo;
	}
	
	/**
	 * Get the fieldInfo of a fieldName
	 *
	 * @param string $fieldName
	 * @return AbstractHtmlFieldInfo
	 */
	function &getFieldHtmlInfo($fieldName) {
		return $this->_arrayFields[$fieldName];
	}
	
	/**
	 * Insert a field into fieldsArray at index.
	 * If 	index: 0 	--> first
	 * 		index: -1 	--> last
	 * 		index: x	--> insert at x
	 *
	 * @param string $fieldName
	 * @param AbstractHtmlFieldInfo $fieldInfo
	 * @param int $index
	 */
	function insertFieldHtmlInfo($fieldName, $fieldInfo, $index = -1) {
		if ($index < 0 || count($this->_arrayFields) <= $index) {
			$this->_arrayFields[$fieldName] = $fieldInfo;
			return;
		}
		
		$resultArr = array();
		$i = 0;
		foreach ($this->_arrayFields as $field => $info) {
			 if ($i == $index) {
			 	$resultArr[$fieldName] = $fieldInfo;
			 }
			 $resultArr[$field] = $info;
			 $i++;
		}
		$this->_arrayFields = &$resultArr;
	}

	/**
	 * Set the link rule for the fieldInfo
	 *
	 * @param string $fieldName field name that want to set the rule
	 * @param string $linkRule the link rule
	 */
	function setLinkRule($fieldName, $linkRule) {
		$field = &$this->_arrayFields[$fieldName];
		$field->setLinkRule($linkRule);
	}
	
	/**
	 * Generate the HTML
	 *
	 * @abstract 
	 * @return string
	 */
	function generateHtml() {
		RubikHelper::printDebugCallStack();
		exit('The function:'.__FUNCTION__.'must ben implement by the subclass');
	}
	
	/**
	 * Print the html
	 *
	 */
	function printHtml() {
		echo $this->generateHtml();
	}
	
	/**
	 * Create the control field name.
	 * NOTE: this function should be override for special cases
	 *
	 * @param string $fieldName
	 * @return string
	 */
	function _getControlName($fieldName) {
		return $fieldName;
	}
	
	/**
	 * Get the lable
	 * NOTE: this function can be override when needed
	 *
	 * @param string $key
	 * @return string
	 */
	function getLabel($key) {
		return SysLanguage::_($key);
	}

}

?>
